package com.softlab.retailwholesalesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softlab.retailwholesalesystem.entity.UserEntity;
import com.softlab.retailwholesalesystem.mapper.UserMapper;
import com.softlab.retailwholesalesystem.service.IUserService;
import com.softlab.retailwholesalesystem.service.exception.InsertRunTimeException;
import com.softlab.retailwholesalesystem.service.exception.NoUserException;
import com.softlab.retailwholesalesystem.service.exception.PasswordWrongException;
import com.softlab.retailwholesalesystem.service.exception.UsernameDuplicatedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements IUserService {

    @Autowired
    UserMapper mapper;

    @Override
    public UserEntity login(UserEntity user) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("u.username",user.getUsername());
        wrapper.eq("u.is_delete",0);
        List<UserEntity> users = mapper.getUsers(wrapper);
        if (users.size() == 0){
            throw new NoUserException("用户名不存在");
        }
        UserEntity existUser=users.get(0);
        String password = user.getPassword();
        String salt = existUser.getSalt();
        password=md5Password(password,salt);
        if (!password.equals(existUser.getPassword())){
            throw new PasswordWrongException("用户名或密码错误");
        }
        UserEntity newUser = new UserEntity();
        newUser.setRoleId(existUser.getRoleId());
        newUser.setUsername(existUser.getUsername());
        newUser.setUserId(existUser.getUserId());
        return newUser;
    }

    @Override
    public void registerUser(UserEntity user) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        wrapper.eq("is_delete",0);
        UserEntity existUser = mapper.selectOne(wrapper);
        if (existUser!=null){
            throw new UsernameDuplicatedException("该用户已存在");
        }
        String password = user.getPassword();
        String salt = UUID.randomUUID().toString().toUpperCase();
        user.setSalt(salt);
        password=md5Password(password,salt);
        user.setPassword(password);
        int insert = mapper.insert(user);
        if (insert!=1){
            throw new InsertRunTimeException("信息注册错误");
        }
    }

    @Override
    public void changePassword(UserEntity user) {
        QueryWrapper<UserEntity> wrapper=new QueryWrapper<>();
        wrapper.eq("username",user.getUsername());
        UserEntity userEntity = mapper.selectOne(wrapper);
        if(userEntity == null){
            throw new NoUserException("无此用户");
        }
        String salt = userEntity.getSalt();
        String password=user.getPassword();
        password=md5Password(password,salt);
        user.setPassword(password);
        user.setUserId(userEntity.getUserId());
        mapper.updateById(user);
    }

    @Override
    public List<UserEntity> getUserByCondition(UserEntity user) {
        QueryWrapper<UserEntity> wrapper = new QueryWrapper<>();
        if (user.getUserId() != null) wrapper.eq("u.user_id",user.getUserId());
        if (user.getUsername() != null) wrapper.eq("u.username",user.getUsername());
        if (user.getRoleName() != null) wrapper.eq("u.role_name",user.getRoleName());
        if (user.getTelephone() != null) wrapper.eq("u.telephone",user.getTelephone());
        if (user.getSex() != null) wrapper.eq("u.sex",user.getSex());
        wrapper.eq("u.is_delete",0);
        return mapper.getUsers(wrapper);
    }

    public String md5Password(String password,String salt) {
        for (int i = 0; i < 3; i++) {
            password= DigestUtils.md5DigestAsHex((salt+password+salt).getBytes()).toUpperCase();
        }
        return password;
    }

}
